﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ProjetSMA.Model
{
    public class World
    {
        private Element[,] m_map_element;
        private int m_length_map = 10;
        private int m_length_item_move = 8;
        private ItemMove[] m_items_move;
        //private int m_number_rocks = 15;
        private List<int> m_number_rocks = new List<int>();
        private List<Element> m_depart = new List<Element>();
        private List<Food> m_food = new List<Food>();


        public World(Element[,] par_map)
        {
            m_map_element = par_map;
            m_items_move = new ItemMove[m_length_item_move];
        }

        public World()
        {
            m_map_element = new Element[m_length_map, m_length_map];
            m_items_move = new ItemMove[m_length_item_move];
        }

        public void InitialisationMap()
        {
            int loc_cpt = 0;

            Random loc_random = new Random();

            for (int i = 0; i < m_length_map * m_length_map; i++)
            {
                if (i % loc_random.Next(1, m_length_map * (m_length_map / 3)) == 0)
                {
                    for (int j = 0; j < loc_random.Next(1, 13); j++)
                    {
                        m_number_rocks.Add(loc_cpt);
                        loc_cpt++;
                    }
                }

                if (loc_cpt < m_length_map * m_length_map)
                    loc_cpt++;

            }


            int par_cpt = 1;
            bool loc_bool =false;
            Random loc_rd = new Random();

            for (int i = 0; i < m_length_map; i++)
            {
                for (int j = 0; j < m_length_map; j++)
                {
                    loc_bool = false;
                    if (loc_rd.Next(0, m_length_map) == (m_length_map/2))
                        loc_bool = true;

                    // underscore between i & j is here to prove the name unicity
                    Element loc_element = new Field(i, j, "_field_" + i + "_" + j, loc_bool);
  
                    for (int k = 0; k < m_number_rocks.Count; k++)
                        if (par_cpt == m_number_rocks[k])
                            loc_element = new Rock(i, j, "_rock_" + i + "_" + j);

                    m_map_element[i, j] = loc_element;
                    m_map_element[i, j].InitialisationTimer();
                    par_cpt++;
                }
            }

            int loc_cpt_2 = 6;

            foreach (ItemMove item in m_items_move)
            {
                if (m_map_element[loc_cpt_2, 10] != null)
                    m_depart.Add(m_map_element[loc_cpt_2, 10]);
                else
                    m_depart.Add(m_map_element[0, 0]);
                loc_cpt_2 += 3;
            }

        }



        public void InitialisationNeighboors()
        {
            for (int i = 0; i < m_length_map; i++)
            {
                for (int j = 0; j < m_length_map; j++)
                {
                    m_map_element[i, j].InitialisationNeighbors(m_map_element, m_length_map);
                }
            }
        }

        public void InitialisationHeights()
        {
            int loc_cpt = 0;
            Random loc_rd = new Random();

            for (int i = 0; i < m_length_map; i++)
            {
                for (int j = 0; j < m_length_map; j++)
                {
                    if (loc_cpt % loc_rd.Next(1, m_length_map * 3) == 0)
                        m_map_element[i, j].Height = loc_rd.Next(3, 14);

                    loc_cpt++;
                }
            }

            for (int i = 0; i < m_length_map; i++)
            {
                for (int j = 0; j < m_length_map; j++)
                {
                    if (m_map_element[i, j].Height > 1)
                        m_map_element[i, j].InitialisationHeight();

                    loc_cpt++;
                }
            }
        }

        public void InitialisationItemMove()
        {
            for (int i = 0; i < m_length_item_move; i++)
            {
                if (m_depart != null)
                {
                    if (i >= (m_length_item_move / 2))
                    {
                        m_items_move[i] = new Prey(m_depart[i], 15, "Prey_" + i);

                    }
                    else
                    {

                        m_items_move[i] = new Predator(m_depart[i], 1, "Predator_" + i);

                    }
                }

            }
        }

        public void InitialisationFood()
        {
            for (int i = 0; i < m_length_map; i++)
            {
                for (int j = 0; j < m_length_map; j++)
                {
                    if(m_map_element[i,j].Food != null)
                        m_food.Add(m_map_element[i, j].Food);
                }
            }
        }

        public int LenghtMap
        {
            get { return m_length_map; }
            set { m_length_map = value; }
        }

        public int LengthItemMove
        {
            get { return m_length_item_move; }
            set { m_length_item_move = value; }
        }

        public Element[,] MapElement
        {
            get { return m_map_element; }
            set { m_map_element = value; }
        }

        public ItemMove[] ItemsMove
        {
            get { return m_items_move; }
            set { m_items_move = value; }
        }

        public List<Food> Food
        {
            get { return m_food; }
            set { m_food = value; }
        }

        private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

    }
}